﻿namespace Harness.Commands
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    internal class FindBrowserCommand : ICommand
    {
        private const int SearchOptionArgIndex = 0;
        private const int SearchValueArgIndex = 1;

        #region Public Methods

        public void SyntaxCheck(IHarness harness, IEnumerable<object> args)
        {
            string searchOption = (args.ElementAtOrDefault(SearchOptionArgIndex) ?? string.Empty).ToString().Trim().ToLower();

            switch (searchOption)
            {
                case "":
                case "all":
                    break;

                case "name":
                case "title":
                case "url":
                    if (null == args.ElementAtOrDefault(SearchValueArgIndex))
                    {
                        throw new ArgumentNullException((SearchValueArgIndex + 1).ToString() + ":Value");
                    }

                    break;

                default:
                    string msg = string.Format(Properties.Resources.Exception_UnknownArgument, (SearchValueArgIndex + 1) + ":" + searchOption);
                    throw new ArgumentException(msg);
            }
        }

        public dynamic Execute(IHarness harness, IEnumerable<object> args)
        {
            string searchOption = (args.ElementAtOrDefault(SearchOptionArgIndex) ?? string.Empty).ToString().Trim().ToLower();
            string value = (args.ElementAtOrDefault(SearchValueArgIndex) ?? string.Empty).ToString();

            switch (searchOption)
            {
                case "":
                case "all":
                    return IE.FindAll();

                case "name":
                    return IE.FindByName(value);

                case "title":
                    return IE.FindByTitle(value);

                case "url":
                    return IE.FindByUrl(value);
            }

            return null;
        }

        #endregion
    }
}
